package com.linkedlist;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.sound.midi.Soundbank;

public class SingLinkedListDemo {
    public static void main(String[] args) {
        //测试
        //先创建节点
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子");

        //创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //加入
//        singleLinkedList.add(hero1);
//        singleLinkedList.add(hero2);
//        singleLinkedList.add(hero3);
//        singleLinkedList.add(hero4);
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero2);

        //测试修改代码
//        HeroNode newHeroNode = new HeroNode(2, "小龙", "玉龙");
//        singleLinkedList.update(newHeroNode);

        //测试删除代码
//        int i = 1;
//        singleLinkedList.delete(i);

        //显示
        singleLinkedList.list();

        //测试有效数据
//        System.out.println(singleLinkedList.Effect(singleLinkedList));
        //测试倒数第K个数据
        System.out.println(singleLinkedList.Break(singleLinkedList,2));
    }
}

//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
    //初始化头节点，不存放具体的数据
    private HeroNode head = new HeroNode(0,"","");

    //求单链表中有效节点的个数
    public int Effect(SingleLinkedList singleLinkedList){
        int i = 1;
        HeroNode temp = head.next;
        if (head.next == null){
            System.out.println("链表为空");
        }
        else {
            while (true){
                if (temp.next == null){
                    break;
                }
                else {
                    temp = temp.next;
                    i++;
                }
            }
        }
        return i;
    }

    //查找单链表中的倒数第k个结点
    public HeroNode Break(SingleLinkedList singleLinkedList,int k){
        int a = 1;
        HeroNode temp1 = head.next;
        HeroNode temp2 = head.next;
        if (head.next == null){
            System.out.println("链表为空");
        }
        else {
            while (true){
                if (temp1.next == null){
                    break;
                }
                else {
                    temp1 = temp1.next;
                    a++;
                }
            }
        }
        if ( k < 0 || k > a){
            System.out.println("找不到该节点");
        }
        else {
            for (int i = 0; i < a - k;i++){
                temp2 = temp2.next;
            }
        }
        return temp2;
    }

    //添加节点到单向链表
    //1.找到当前链表的最后节点
    //2.将最后节点的next指向新的节点
    public void add(HeroNode heroNode){
        //head节点不能动
        HeroNode temp = head;
        //遍历链表，找到最后节点
        while (true){
            //找到链表的最后
            if (temp.next == null){
                break;
            }
            else {
                temp = temp.next;
            }
        }
        //退出while循环时,temp指向链表的最后
        temp.next = heroNode;
    }

    //第二种方式在添加英雄时，根据排名将英雄插入到指定位置
    //如果有这个排名，则添加失败，并给出提示
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;//标识添加的英雄编号是否存在
        while (true){
            if (temp.next == null) {//说明temp已经在链表最后
                break;
            }
            if (temp.next.no > heroNode.no){
                break;
            }
            else if (temp.next.no == heroNode.no){//说明英雄编号已经存在
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //判断flag的值
        if (flag){//编号存在，不能加入
            System.out.println("英雄已经存在，不能加入" + heroNode.no);
        }
        else {
            //
            heroNode.next = temp.next;
            temp.next = heroNode;
        }

    }

    //修改节点的信息，根据newHeroNode的no来修改
    public void update(HeroNode newHeroNode){
        HeroNode temp = head.next;
        boolean flag = false;
        //判断是否为空
        if (head.next == null){
            System.out.println("链表为空");
        }
        else {
            while (true){
                if (temp == null){//说明链表已经遍历结束
                    break;
                }
                if (newHeroNode.no == temp.no){ ;
                    flag = true;
                    break;
                }
                temp = temp.next;
            }
        }
        if (flag){//根据flag判断是否找到要修改的节点
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
            System.out.println("修改成功");
        }
        else {
            System.out.println("没有找到编号的信息");
        }
    }

    //删除节点
    public void delete(int no){
        HeroNode temp = head;
        boolean flag = false;
        if (head.next == null){
            System.out.println("链表为空");
        }
        else {
            while (true){
                if (temp.next == null){
                    break;
                }
                else if (temp.next.no == no){
                    flag = true;
                    break;
                }
                temp = temp.next;
            }
        }
        if (flag){
            temp.next = temp.next.next;
            System.out.println("删除成功");
        }
        else {
            System.out.println("没有找到该节点");
        }
    }

    //遍历链表并显示
    public void list(){
        //判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        else {
            HeroNode temp = head.next;
            while (true){
                //判断是否到链表最后
                if (temp == null){
                    break;
                }
                //输出节点的信息
                else {
                    System.out.println(temp);
                }
                //将temp后移
                temp = temp.next;
            }
        }
    }
}

//定义一个HeroNode,每一个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int hNo, String hName, String hNickname) {
        this.no = hNo;
        this.name = hName;
        this.nickname = hNickname;
    }

    //为了显示方便，重写toString
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname +
                '}';
    }
}